package com.yq.ylmall.product.controller.admin;


import cn.hutool.core.bean.BeanUtil;
import com.yq.ylmall.dto.product.CategoryBrandNameQueryDto;
import com.yq.ylmall.dto.product.CategoryBrandQueryDto;
import com.yq.ylmall.model.product.CategoryBrand;
import com.yq.ylmall.product.service.ICategoryBrandService;
import com.yq.ylmall.result.Result;
import com.yq.ylmall.result.ResultCode;
import com.yq.ylmall.vo.product.BrandVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 分类品牌 前端控制器
 * </p>
 *
 * @author yzk789
 * @since 2024-05-31
 */
@Api(tags = "分类商品品牌接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/product/categoryBrand")
public class CategoryBrandAdminController {

    private final ICategoryBrandService categoryBrandService;

    @ApiOperation(value = "分页条件查询商品品牌")
    @PostMapping("/page")
    public Result<Object> selectCategoryBrandPage(@RequestBody CategoryBrandNameQueryDto dto) {
        return Result.ok(categoryBrandService.queryCategoryBrand(dto));
    }

    @ApiOperation(value = "新增分类商品品牌")
    @PostMapping("/add")
    public Result<Object> saveCategoryBrand(@RequestBody CategoryBrandQueryDto dto) {
        CategoryBrand categoryBrand = BeanUtil.copyProperties(dto, CategoryBrand.class);
        return Result.ok(categoryBrandService.save(categoryBrand));
    }

    @ApiOperation(value = "删除分类商品品牌")
    @DeleteMapping("/remove/{id}")
    public Result<Object> removeCategoryBrandById(@PathVariable Long id) {
        boolean success = categoryBrandService.removeById(id);
        return success ? Result.ok(ResultCode.DELETE_SUCCESS) : Result.fail(ResultCode.DELETE_FAIL);
    }

    @ApiOperation(value = "批量删除分类商品品牌")
    @DeleteMapping("/remove/batch")
    public Result<Object> removeCategoryBrandByIds(List<Long> ids) {
        boolean success = categoryBrandService.removeByIds(ids);
        return success ? Result.ok(ResultCode.DELETE_SUCCESS) : Result.fail(ResultCode.DELETE_FAIL);
    }

    @ApiOperation(value = "修改分类商品品牌")
    @PostMapping("/update")
    public Result<Object> updateCategoryBrandById(@RequestBody CategoryBrand dto) {
        boolean success = categoryBrandService.doUpdate(dto);
        return success ? Result.ok(ResultCode.SAVE_SUCCESS) : Result.fail(ResultCode.SAVE_FAIL);
    }

    @ApiOperation(value = "根据分类ID查找品牌")
    @GetMapping("/get/brand/{categoryId}")
    public Result<List<BrandVo>> getBrandByCategoryId(@PathVariable Long categoryId) {
        return Result.ok(categoryBrandService.getBrandByCategoryId(categoryId));
    }


    @ApiOperation(value = "根据子ID查找父类")
    @GetMapping("/get/categoryParentID/{categoryId}")
    public Result<List<Long>> getParentByCategoryId(@PathVariable Long categoryId) {
        List<Long> parentCategoryIds = categoryBrandService.getParentCategoryIds(categoryId);
        return Result.ok(parentCategoryIds);
    }

}
